<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="(ruleForm.id !== 0 ? '修改' : '添加') + '用户'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="ruleForm.id === 0">
						<el-form-item prop="userName">
							<template #label>
								<el-tooltip content="用户名长度需大于6位，否则报参数错误" placement="top-start">
									<el-icon style="height: 100%">
										<ele-InfoFilled />
									</el-icon>
								</el-tooltip>
								用户名
							</template>
							<el-input v-model="ruleForm.userName" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="ruleForm.id === 0">
						<el-form-item prop="userPassword">
							<template #label>
								<el-tooltip content="密码需包含字母、数字和特殊字符" placement="top-start">
									<el-icon style="height: 100%">
										<ele-InfoFilled />
									</el-icon>
								</el-tooltip>
								账户密码
							</template>
							<el-input v-model="ruleForm.userPassword" placeholder="请输入" type="password" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户昵称" prop="userNickname">
							<el-input v-model="ruleForm.userNickname" placeholder="请输入用户昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="mobile">
							<el-input v-model="ruleForm.mobile" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门" prop="deptId">
							<el-cascader
								:options="deptTree"
								:props="{ checkStrictly: true, emitPath: false, value: 'deptId', label: 'deptName' }"
								placeholder="请选择部门"
								clearable
								class="w100"
								v-model="ruleForm.deptId"
							>
								<template #default="{ node, data }">
									<span>{{ data.deptName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="关联角色" prop="roleIds">
							<el-select v-model="ruleForm.roleIds" placeholder="请选择" clearable class="w100" multiple>
								<el-option v-for="role in roleList" :key="'role-' + role.id" :label="role.name" :value="role.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱" prop="userEmail">
							<el-input v-model="ruleForm.userEmail" placeholder="请输入邮箱" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="性别" prop="sexStr">
							<el-select v-model="ruleForm.sexStr" placeholder="请选择性别" clearable class="w100">
								<el-option v-for="gender in genderSelect" :key="'gender-' + gender.value" :label="gender.label" :value="gender.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="岗位" prop="postIds">
							<el-select v-model="ruleForm.postIds" placeholder="请选择岗位" clearable class="w100" multiple>
								<el-option v-for="post in postList" :key="'post-' + post.postId" :label="post.postName" :value="post.postId"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
							<el-switch
								v-model="ruleForm.userStatus"
								inline-prompt
								:active-value="1"
								:inactive-value="0"
								active-text="启"
								inactive-text="禁"
							></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="用户描述">
							<el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入用户描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.id !== 0 ? '修 改' : '添 加' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, toRefs, unref } from 'vue';
import { addUser, editUser, getUserData, getUserParams } from '/@/api/system/user';
import { ElMessage } from 'element-plus';

export default defineComponent({
	name: 'systemEditUser',
	props: {
		deptTree: {
			type: Array,
			default: () => [],
		},
		genderSelect: {
			type: Array,
			default: () => [],
		},
	},
	setup(prop, { emit }) {
		const roleList = ref([]);
		const postList = ref([]);
		const formRef = ref<HTMLElement | null>(null);
		const state = reactive({
			isShowDialog: false,
			ruleForm: {
				id: 0,
				userName: '',
				mobile: '',
				userNickname: '',
				birthday: 0,
				userPassword: '',
				userStatus: 1,
				userEmail: '',
				sex: 0,
				sexStr: '0',
				avatar: '',
				deptId: 0,
				remark: '',
				userType: 1,
				Address: '',
				Describe: '',
				postIds: [],
				roleIds: [],
			},
			//表单校验
			rules: {
				userName: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }],
				userNickname: [{ required: true, message: '用户昵称不能为空', trigger: 'blur' }],
				userPassword: [{ required: true, message: '用户密码不能为空', trigger: 'blur' }],
				userEmail: [
					{ required: true, message: '邮箱不能为空', trigger: 'blur' },
					{
						type: 'email',
						message: "'请输入正确的邮箱地址",
						trigger: ['blur', 'change'],
					},
				],
				mobile: [
					{ required: true, message: '手机号码不能为空', trigger: 'blur' },
					{
						pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
						message: '请输入正确的手机号码',
						trigger: 'blur',
					},
				],
			},
		});
		// 打开弹窗
		const openDialog = (row?: any) => {
			resetForm();
			if (row) {
				getUserData(row.id).then((res: any) => {
					const user = res.data.user;
					state.ruleForm = {
						id: user.id,
						userName: user.userName,
						mobile: user.mobile,
						userNickname: user.userNickname,
						birthday: user.birthday,
						userPassword: '-',
						userStatus: user.userStatus,
						userEmail: user.userEmail,
						sex: user.sex,
						sexStr: String(user.sex),
						avatar: user.avatar,
						deptId: user.deptId,
						remark: user.remark,
						userType: user.userType,
						Address: user.address,
						Describe: user.describe,
						postIds: res.data.checkedPostIds ?? [],
						roleIds: res.data.checkedRoleIds ?? [],
					};
				});
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			emit('getUserList');
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					state.ruleForm.sex = parseInt(state.ruleForm.sexStr);
					if (state.ruleForm.id === 0) {
						//添加
						addUser(state.ruleForm).then(() => {
							ElMessage.success('用户添加成功');
							closeDialog(); // 关闭弹窗
						});
					} else {
						//修改
						editUser(state.ruleForm).then(() => {
							ElMessage.success('用户修改成功');
							closeDialog(); // 关闭弹窗
						});
					}
				}
			});
		};
		// 初始化部门数据
		const initTableData = () => {
			//获取角色岗位选项
			getUserParams().then((res: any) => {
				roleList.value = res.data.roleList ?? [];
				postList.value = res.data.postList ?? [];
			});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		const resetForm = () => {
			state.ruleForm = {
				id: 0,
				userName: '',
				mobile: '',
				userNickname: '',
				birthday: 0,
				userPassword: '',
				userStatus: 1,
				userEmail: '',
				sex: 0,
				sexStr: '0',
				avatar: '',
				deptId: 0,
				remark: '',
				userType: 1,
				Address: '',
				Describe: '',
				postIds: [],
				roleIds: [],
			};
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			roleList,
			postList,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
